package com.szhuang.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.ui.SettingItemView;

/**
 * Created by szhuang on 2015/3/21.
 */
public class SettingActivity extends Activity {

    private SharedPreferences sp;
    private SettingItemView updateItem;
    private TextView aboutSoftware;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();



        setListener();

    }

    public void init(){
        updateItem=(SettingItemView)findViewById(R.id.updateItem);
        sp=getSharedPreferences("config",MODE_PRIVATE);
        aboutSoftware=(TextView)findViewById(R.id.aboutSoftware);

        updateItem.setTitle("自动更新是否开启");
        if(sp.getBoolean("update",true)){
            updateItem.setChecked(true);
            updateItem.setDesc("自动更新已经开启");
        }else{
            updateItem.setChecked(false);
            updateItem.setDesc("自动更新已经关闭");
        }
    }

    public void setListener(){
        updateItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor=sp.edit();
                if(updateItem.isOpen()){
                    updateItem.setChecked(false);
                    updateItem.setDesc("自动更新已经关闭");
                    editor.putBoolean("update",false);
                }else{
                    updateItem.setChecked(true);
                    updateItem.setDesc("自动更新已经开启");
                    editor.putBoolean("update",true);
                }
                editor.commit();
            }
        });

        aboutSoftware.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SettingActivity.this,AboutSoftwareActivity.class);
                startActivity(intent);
            }
        });
    }
}
